//
//  NewFeatureCollectionController.swift
//  XDweibo
//
//  Created by HanXingda on 15/6/28.
//  Copyright © 2015年 韩兴达. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionController: UICollectionViewController {

    //设置页面布局
    let layout = UICollectionViewFlowLayout()
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }
    //设置页面的布局
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }

    // MARK: UICollectionViewDataSource

   let imageCount = 4


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCollectionViewCell
        
        cell.imageIndex = indexPath.item
    
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取当前显示的Cell
        let path = collectionView.indexPathsForVisibleItems().last
        //判断当前的图片页数
        if path?.item == imageCount - 1 {
            //获取当前的cell
            (collectionView.cellForItemAtIndexPath(path!) as! NewFeatureCollectionViewCell).showIconButton()
        }
    }

  
}

class NewFeatureCollectionViewCell: UICollectionViewCell {
    var imageIndex = 0 {
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            iconButton.hidden = true
        }
    }
    /// 点击开始按钮
    func startButtonClicked() {
        
        // object = true 表示显示 MainViewController
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchViewControllerNotifocation, object: true)
    }

    func showIconButton() {
        
        iconButton.hidden = false
        iconButton.transform = CGAffineTransformMakeScale(0, 0)
        iconButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.iconButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.iconButton.userInteractionEnabled = true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconImage)
        addSubview(iconButton)

        iconButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        iconImage.frame = self.bounds
    }
    
    //懒加载 
    //界面图片
    lazy var iconImage: UIImageView = {
        return UIImageView()
    }()
    //按钮
    lazy var iconButton: UIButton = {
       let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.addTarget(self, action: "startButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
